/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package net.synck.gcontacts.editor;
import net.synck.gcontacts.tools.RelTools;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.forms.*;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.*;

import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.extensions.Email;
import com.google.gdata.data.extensions.Im;
import com.google.gdata.data.extensions.PhoneNumber;
import com.google.gdata.data.extensions.PostalAddress;
/**
 *
 */
public class ContactsBlock extends MasterDetailsBlock {
	public enum ContactCategory {		
		EMAIL,
		PHONE,
		IM,
		ADDRESS
	}
	
	private FormPage page;
	
	public ContactsBlock(FormPage page) {
		this.page = page;		
	}
		
	
	class MasterContentProvider implements ITreeContentProvider {

		ContactEntry input = null;
		
		@Override
		public Object[] getChildren(Object parentElement) {
			if (parentElement instanceof ContactCategory) {
				ContactCategory category = (ContactCategory)parentElement;
				
				if (category == ContactCategory.EMAIL) {					
					return input.getEmailAddresses().toArray();
				} else if (category == ContactCategory.PHONE) {
					return input.getPhoneNumbers().toArray();
				} else if (category == ContactCategory.IM) {
					return input.getImAddresses().toArray();
				} else if (category == ContactCategory.ADDRESS) {
					return input.getPostalAddresses().toArray();
				}
			}
			return null;
		}
				
		@Override
		public Object getParent(Object element) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public boolean hasChildren(Object element) {			
			if (element instanceof ContactCategory) {
				ContactCategory category = (ContactCategory)element;				
				if (category == ContactCategory.EMAIL) {
					return !input.getEmailAddresses().isEmpty();
				} else if (category == ContactCategory.PHONE) {
					return !input.getPhoneNumbers().isEmpty();
				} else if (category == ContactCategory.IM) {
					return !input.getImAddresses().isEmpty();
				} else if (category == ContactCategory.ADDRESS) {
					return !input.getPostalAddresses().isEmpty();
				}
			}
			return false;
		}

		@Override
		public Object[] getElements(Object inputElement) {
			return ContactCategory.values();
		}

		@Override
		public void dispose() {
			// TODO Auto-generated method stub
		}

		@Override
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			//TODO fix type check and binding
			input = (ContactEntry)newInput;
		}

	}
	
	class MasterLabelProvider extends LabelProvider {
		
		public String getText(Object obj) {
			
			if (obj instanceof ContactCategory) {
				return obj.toString();
			}
			
			if (obj instanceof Email) {
				Email email = (Email)obj;								
				return email.getAddress() 
					+ (email.getRel() != null ? " (" + RelTools.getEmailRel().get(email.getRel()) + ")" : "" );				
			}
			
			if (obj instanceof PhoneNumber) {
				PhoneNumber phoneNumber = (PhoneNumber)obj;				
				return phoneNumber.getPhoneNumber() 
					+ (phoneNumber.getRel() != null ? " (" + RelTools.getPhoneNumberRel().get(phoneNumber.getRel()) + ")" : "" );				
			}
			
			if (obj instanceof Im) {
				Im im = (Im)obj;				
				return im.getAddress()
				+ (" [" + RelTools.getImProtocol().get(im. getProtocol()) + "]")
				+ (im.getRel() != null ? " ("  + RelTools.getImRel().get(im.getRel()) + ")" : "" );				
			}

			if (obj instanceof PostalAddress) {
				PostalAddress address = (PostalAddress)obj;				
				return address.getValue()				
				+ (address.getRel() != null ? " (" + RelTools.getPostalAddressRel().get(address.getRel()) + ")" : "" );				
			}

						
			return null;
		}
		public Image getColumnImage(Object obj, int index) {
			/*
			if (obj instanceof TypeOne) {
				return PlatformUI.getWorkbench().getSharedImages().getImage(
						ISharedImages.IMG_OBJ_ELEMENT);
			}
			if (obj instanceof TypeTwo) {
				return PlatformUI.getWorkbench().getSharedImages().getImage(
						ISharedImages.IMG_OBJ_FILE);
			}
			*/
			return null;
		}
	}
	protected void createMasterPart(final IManagedForm managedForm,
			Composite parent) {
		//final ScrolledForm form = managedForm.getForm();
		FormToolkit toolkit = managedForm.getToolkit();
		Section section = toolkit.createSection(parent, Section.DESCRIPTION|Section.TITLE_BAR);
		section.setText("Contacts"); //$NON-NLS-1$
		section
				.setDescription("Contact details"); //$NON-NLS-1$
		section.marginWidth = 10;
		section.marginHeight = 5;
		Composite client = toolkit.createComposite(section, SWT.WRAP);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		layout.marginWidth = 2;
		layout.marginHeight = 2;
		client.setLayout(layout);
		Tree t = toolkit.createTree(client, SWT.NULL);
		GridData gd = new GridData(GridData.FILL_BOTH);
		gd.heightHint = 20;
		gd.widthHint = 100;
		t.setLayoutData(gd);
		toolkit.paintBordersFor(client);
		Button b = toolkit.createButton(client, "Add", SWT.PUSH); //$NON-NLS-1$
		gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
		b.setLayoutData(gd);
		section.setClient(client);
		final SectionPart spart = new SectionPart(section);
		managedForm.addPart(spart);
		TreeViewer viewer = new TreeViewer(t);
		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				managedForm.fireSelectionChanged(spart, event.getSelection());
			}
		});
		viewer.setContentProvider(new MasterContentProvider());
		viewer.setLabelProvider(new MasterLabelProvider());
		viewer.setInput(((ContactEditorInput)page.getEditor().getEditorInput()).getContactEntry());
	}
	protected void createToolBarActions(IManagedForm managedForm) {
		final ScrolledForm form = managedForm.getForm();
		Action haction = new Action("hor", Action.AS_RADIO_BUTTON) { //$NON-NLS-1$
			public void run() {
				sashForm.setOrientation(SWT.HORIZONTAL);
				form.reflow(true);
			}
		};
		haction.setChecked(true);
//		haction.setToolTipText(Messages.getString("ScrolledPropertiesBlock.horizontal")); //$NON-NLS-1$
		
//		haction.setImageDescriptor(FormArticlePlugin.getDefault()
//				.getImageRegistry()
//				.getDescriptor(FormArticlePlugin.IMG_HORIZONTAL));
		
		Action vaction = new Action("ver", Action.AS_RADIO_BUTTON) { //$NON-NLS-1$
			public void run() {
				sashForm.setOrientation(SWT.VERTICAL);
				form.reflow(true);
			}
		};
		vaction.setChecked(false);
//		vaction.setToolTipText(Messages.getString("ScrolledPropertiesBlock.vertical")); //$NON-NLS-1$
//		vaction.setImageDescriptor(FormArticlePlugin.getDefault()
//				.getImageRegistry().getDescriptor(FormArticlePlugin.IMG_VERTICAL));
		form.getToolBarManager().add(haction);
		form.getToolBarManager().add(vaction);
	}
	protected void registerPages(DetailsPart detailsPart) {
		detailsPart.registerPage(Email.class, new EmailDetailsPage());
//		detailsPart.registerPage(TypeTwo.class, new TypeTwoDetailsPage());
	}
}